package com.tul.exercise.base.jmockit.mockpublicmethod;

import com.tul.exercise.base.jmockit.mockpublicmethod.itf.Demo1DAO;
import com.tul.exercise.base.jmockit.mockpublicmethod.itf.Demo1Service;
import mockit.Expectations;
import mockit.Injectable;
import mockit.Tested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * 《JMockit中文版入门指南》 1.4 Mock 普通公有方法
 * Demo1ServiceImpl测试类
 *
 * @author tuliang
 */
// jmockit 与 SpringJUnit联合使用时，可以直接使用@RunWith(SpringJUnit4ClassRunner.class)
// pom文件中 jmockit依赖要在junit之前

// 可以指定xml配置文件或者基于java的配置类
@ContextConfiguration(classes = SpringConfig.class)
public class Demo1ServiceImplTest {

    @Tested
    @Qualifier("demo1Service")
    @Autowired
    Demo1Service demo1Service;

    @Injectable
    //@Mocked
    @Qualifier("demo1DAO")
    @Autowired
    Demo1DAO demo1DAO;

    // 不录制具体的方法，返回mock的默认值
    @Test
    public void testService_default() {
        // 被mock的方法，根据返回值类型，默认的值不同，返回值类型为对象的，默认null，
        // 返回值为数字的，默认为0
        // 返回值为boolean 的默认为false
        assertTrue(demo1Service.sayName() == null);
        assertTrue(demo1Service.sayAge() == 0);
        assertTrue(demo1Service.isTp() == false);
    }


    @Test
    public void testService() {

        String name = "mayun";
        int age = 18;
        new Expectations(){{
            demo1DAO.getName();
            result = "mayun";

            demo1DAO.getAge();
            result = age;
        }};

        System.out.println(demo1Service.sayName());
        // ??
        assertEquals(demo1Service.sayName(), name);

        System.out.println(demo1Service.sayAge());
        // ??
        assertEquals(demo1Service.sayAge(), age);

    }
}
